<template>
  <div class="app-container">
    <el-row :gutter="0">
      <!--专病数据-->
      <el-form class="formBox searchForm" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
               label-width="68px">
        <el-form-item label="医院" prop="hospital" clearable>
          <department :isHospital="true" :isdepat="true" @refreshData="refreshData" deptWidth="200px"
                      ref="department"></department>
        </el-form-item>
        <el-form-item label="用户类别" prop="isPatient" >
          <el-select v-model="queryParams.isPatient" clearable placeholder="类别">
            <el-option label="医生" :value="0"></el-option>
            <el-option label="病人" :value="1"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>


      <div class="formBox searchForm" ref="statisticalTable" style="margin-bottom:0px!important;">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                       v-hasPermi="['system:phrase:add']">新增</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="phrasesList" border>
          <el-table-column sortable label="编号" align="center" prop="id" width="100" />
          <el-table-column sortable label="用户类别" align="center" prop="id" width="100" >
            <template slot-scope="scope">
              <span>{{ (scope.row.isPatient===1?'病人':'医生') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="医院" align="center" prop="hospitalName" :show-overflow-tooltip="true"
                           width="150">
            <template slot-scope="scope">
              <departTag :options="deptNameLists" :value="scope.row.hospitalId" />
            </template>
          </el-table-column>
          <el-table-column label="科室" align="center" prop="deptName" :show-overflow-tooltip="true" width="150">
            <template slot-scope="scope">
              <departTag :options="deptNameLists" :value="scope.row.deptId" />
            </template>
          </el-table-column>

          <el-table-column label="常用语" align="center" prop="phrases" :show-overflow-tooltip="true"/>
          <el-table-column sortable label="创建时间" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="160"
                           class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                         v-hasPermi="['system:phrase:edit']">修改</el-button>

              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                         v-hasPermi="['system:phrase:delete']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getList" />


      </div>

    </el-row>


    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :before-close="cancel"
               :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="left" class="formBox1">
        <el-row>
          <el-col :span="24">
            <el-form-item label="使用科室" prop="deptId" v-if="open">
              <department :isAdd="true" @changedepatData="changedepatData" :HospitalId="form.hospitalId"
                          :deptIds="form.deptId" :isdisabled="false"></department>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户类别" prop="isPatient">
              <el-select v-model="form.isPatient" clearable placeholder="类别">
                <el-option label="医生" :value="0"></el-option>
                <el-option label="病人" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="常用语" prop="phrases">
              <el-input type="textarea" v-model="form.phrases"></el-input>
            </el-form-item>
          </el-col>
        </el-row>





      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="submitForm">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>

</template>

<script>
import department from "@/views/components/department/index.vue";
import departTag from "@/views/components/department/departTag.vue";
import {deptNameList} from "@/api/system/dept";
import {addPhrases, deletePhrases, listPhrases, updatePhrases} from "@/api/system/phrase";
import {delDevice, getDevice} from "@/api/system/device";

export default {
  name: "phrase",
  components: { department,departTag },
  data(){
    return{
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hospitalId: undefined,
        deptId: undefined,
        isPatient:'',
      },
      deptNameLists: [],
      form :{
        deptId: undefined,
        hospitalId: undefined,
        isPatient: '',
        phrases:'',
      },
      // 表单校验
      rules: {
        isPatient: [
          {
            required: true,
            message: "用户类别必选",
            trigger: "blur",
          },
        ],
        phrases: [
          {
            required: true,
            message: "常用语必填",
            trigger: "blur",
          },
        ],
        deptId: [
          {
            required: true,
            message: "科室必选",
            trigger: "blur",
          },
        ],
      },
      phrasesList:[],
    }
  },
  methods:{
    refreshData(data) {
      this.queryParams.pageNum = 1;
      this.queryParams.hospitalId = data.hospitalId
      this.queryParams.deptId = data.deptId
      this.getdeptNameList();
    },
    changedepatData(data) {
      if (data && data.length > 0) {
        this.form.deptId = data[0].deptId
        this.form.hospitalId = data[0].parentId
      } else {
        this.form.deptId = undefined
        this.form.hospitalId = undefined
      }
    },
    // 回显医院及门诊
    getdeptNameList() {
      deptNameList().then((response) => {
        console.log(response.data)
        this.deptNameLists = response.data;
        this.getList();
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      if (this.$refs.department) {
        this.$refs.department.reset()
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增常用语";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form=row
      this.title = "修改设备";
      this.open = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm(
          '是否确认删除该数据项？'
        )
        .then(function () {
          return deletePhrases(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: undefined,
        hospitalId: undefined,
        isPatient: '',
        phrases:'',
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function () {

      console.log(this.form.detectionIds)
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updatePhrases(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPhrases(this.form).then((response) => {
              console.log('addPhrases---',response)
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listPhrases(
        this.queryParams
      ).then((response) => {
        console.log('getList--response',response)
        this.phrasesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },


  }

}
</script>

<style scoped>

</style>
